package com.googlecode.jyoutubeuploader.uploader;

import java.io.File;
import java.io.FileNotFoundException;

import javax.activation.MimetypesFileTypeMap;

import org.apache.log4j.Logger;

import com.googlecode.jyoutubeuploader.to.YouTubeArgumentsTo;
import com.googlecode.jyoutubeuploader.util.StatisticsCalculator;

public class ThrottledMediaFileSourceFactory {

	private Logger logger;
	private StatisticsCalculator statisticsCalculator;
	
	public ThrottledMediaFileSource getThrottledMediaFileSource(YouTubeArgumentsTo args) {
		File mediaFile = args.getFile();
		try {
			ThrottledFileInputStream stream = new ThrottledFileInputStream(mediaFile, args.getThrottleSpeed(), args.getReportingInterval());
			stream.setLogger(logger);
			stream.setStatisticsCalculator(statisticsCalculator);
			String mediaType = new MimetypesFileTypeMap().getContentType(mediaFile);
			logger.debug("Determined content type: " + mediaType);
			ThrottledMediaFileSource mediaFileSource = new ThrottledMediaFileSource(mediaFile, mediaType, stream);
			return mediaFileSource;
		} catch (FileNotFoundException e) {
			logger.fatal("Could not open the supplied file: " + mediaFile.getName(), e);
			throw new RuntimeException("");
		}
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setStatisticsCalculator(StatisticsCalculator statisticsCalculator) {
		this.statisticsCalculator = statisticsCalculator;
	}
}
